
var prodId = art.dialog.data('productId');
//prodId = 111;

Vue.component('add-button',{
    props:['coupon'],
    template: '<div>{{ coupon.cardDesc }} ' +
        '<button v-on:click="addcoupon" class="fa fa-plus-circle" style="color: #449d44"></button> ' +
        '</div>',
    methods: {
        addcoupon: function () {
            console.log(this.coupon.cardDesc);
            this.$emit('addcoupon');
        }
    }
});

Vue.component('remove-button',{
    props:['coupon'],
    template: '<div>{{ coupon.cardDesc }} ' +
            '<button v-on:click="removecoupon" class="fa fa-minus-circle" style="color: #ce132d"></button> ' +
            '</div>',
    methods: {
        removecoupon: function () {
            console.log(this.coupon.cardDesc);
            this.$emit('removecoupon');
        }
    }
});

var vm = new Vue({
    el: '#ProductCoupon',
    data: {
        msg: '',
        show: false,
        alertClass: 'alert',
        alertClassSucc: 'alert-success',
        alertClassWarn: 'alert-danger',
        isAlert: false,
        isDebug: false,

        productId:prodId,
        productName:art.dialog.data('productName'),
        filter:'',
        coupons: {},
        selectCoupons: {},
    },
    created: function () {
        this.$http.get('/app/productCoupon/list/'+this.productId).then(
            response => {
            this.coupons ='';

        var data = response.body;

        if (data.resultCode != '200') {
            this.showMsg(data.msg,true);
            return;
        }

        if (data.result == null || data.result.length == 0) {
            this.loadAll();
            return;
        }

        var selectedCoupon={};
        for (var i = 0; i < data.result.length; i++) {

            var singleObj = data.result[i];
            //selectedCoupon.push(singleObj);
            selectedCoupon[singleObj.cardid]=singleObj;
        }

        if(this.isDebug)
            console.log(selectedCoupon);

        this.selectCoupons = selectedCoupon;
        this.showMsg ('加载完毕');

        this.loadAll();
        //console.log(response);
        });

    },
    methods: {
        loadAll: function () {
            this.$http.get('/app/productCoupon/list?productId='+this.productId+'&filter='+this.filter).then(
            response => {
                this.coupons ='';

                var data = response.body;

                if (data.resultCode != '200') {
                    this.showMsg(data.msg,true);
                    return;
                }

                if (data.result == null || data.result.length == 0) {
                    this.showMsg('查无数据',true);
                    return;
                }

            var filterCoupons={};
            for (var i = 0; i < data.result.length; i++) {

                var singleObj = data.result[i];

                var cardid = singleObj.cardid.toString();
                if(this.selectCoupons[cardid] == null)
                    filterCoupons[cardid]=singleObj;
            }

            this.coupons = filterCoupons;
            this.showMsg ('加载完毕');

            if(this.isDebug)
                console.log(filterCoupons);
            });


        } ,
        addSelectCoupon:function(coupon){
            if(this.isDebug)
                console.log(coupon.cardid);

            Vue.set(this.selectCoupons, coupon.cardid, coupon);
            Vue.delete(this.coupons, coupon.cardid);
        },
        removeSelectCoupon:function(coupon){
            if(this.isDebug)
                console.log(coupon.cardid);

            Vue.delete(this.selectCoupons, coupon.cardid);

            if(this.isDebug)
                console.log(this.coupons);

            Vue.set(this.coupons, coupon.cardid, coupon);
        },
        save: function(){
            var json = JSON.parse('{ "productId": "", "cardIds": "" }');
            json.productId = this.productId;
            var cardIds=[];
            for(x in this.selectCoupons){
                cardIds.push(this.selectCoupons[x].cardid.toString());
            }

            json.cardIds= cardIds;

            if(this.isDebug)
                console.log(JSON.stringify(json));

            this.$http.post('/app/productCoupon/update',json).then(
                response => {

            var data = response.body;

            if (data.resultCode != '200') {
                this.showMsg(data.msg,true);
                return;
            }

            if (data.result == null || data.result.length == 0) {
                this.showMsg('保存失败',true);
                return;
            }

            this.showMsg('保存: '+data.msg+'!');

            });
        },
        showMsg: function (msg,isAlert) {
            this.msg = msg;
            this.show = true;

            if(this.isDebug)
                console.log(isAlert);

            if (isAlert)
                this.isAlert = true;
            else
                this.isAlert = false;

            setTimeout(function () {
                this.show = false;
            }.bind(this), 3000);
        }
    }
})



